package com.example.lib.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
import com.example.lib.extend.dip2px
import com.example.lib.extend.getDeviceHeight
import com.example.lib.extend.getDeviceWidth
import com.example.lib.utils.statusbar.StatusBarUtils

class MoveTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    AppCompatTextView(context, attrs, defStyleAttr) {
    private lateinit var mParams: RelativeLayout.LayoutParams
    private var lastX = 0
    private var lastY = 0
    private var widowWidth = 0
    private var widowHeight = 0
    private val marginLR: Int = context.dip2px(16f)
    private val mMarginTop: Int = context.dip2px(5f)
    private val moveMarginLR: Int = context.dip2px(45f)
    private val homeTitleHeight: Int = context.dip2px(50f)
    private var statusBarHeight = StatusBarUtils.getStateBarHeight(context)
    private val navigatorBarHeight = StatusBarUtils.getNavigationBarHeight(getContext())
    private val homeBottomHeight: Int = context.dip2px(75f)
    private var mLeft = 0
    private var mTop = 0
    private var mRight = 0
    private var mBottom = 0
    private var lastLeft = 0
    private var lastTop = 0
    private var lastRight = 0
    private var lastBottom = 0
    var moveLeft: Int = 0
    var moveRight: Int = 0
    private var isMove = false

    init {
        widowWidth = getDeviceWidth(context)
        widowHeight = getDeviceHeight(context = context)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val x = event.x.toInt()
        val y = event.y.toInt()
        val offsetX: Int
        var offsetY = 0
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                isMove = false
                lastX = x
                lastY = y
            }

            MotionEvent.ACTION_MOVE -> {
                offsetX = x - lastX
                offsetY = y - lastY
                if (offsetX != 0 && offsetY != 0) {
                    isMove = true
                }
                setRelativeViewLocation(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)
            }

            MotionEvent.ACTION_UP -> {
                if (!isMove) {
                    performClick()
                    return true
                }
                (event.rawY - (height.toFloat() / 2)).let {
                    mLeft = (widowWidth - marginLR - width)
                    mRight = widowWidth - marginLR

                    if (it < statusBarHeight + homeTitleHeight) {
                        mTop = mMarginTop
                        mBottom = height + mMarginTop
                    } else if (it > widowHeight - navigatorBarHeight - (height / 2)) {
                        mTop = widowHeight - homeBottomHeight - navigatorBarHeight - height
                        mBottom = widowHeight - homeBottomHeight - navigatorBarHeight
                    } else {
                        mTop = top + offsetY
                        mBottom = bottom + offsetY
                    }

                    setRelativeViewLocation(mLeft, mTop, mRight, mBottom)
                }

            }
        }
        return true
    }

    private fun setRelativeViewLocation(left: Int, top: Int, right: Int, bottom: Int) {
        if (lastLeft == left && lastTop == top && lastRight == right && lastBottom == bottom) return
        if (this@MoveTextView::mParams.isInitialized.not()) {
            mParams = RelativeLayout.LayoutParams(right - left, bottom - top)
            mParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE)
            mParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE)
            mParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE)
            mParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE)
        }
        val parentView = parent as View
        val marginRight = parentView.width - right
        val marginBottom = parentView.height - bottom
        mParams.setMargins(left, top, marginRight, marginBottom)
        setLayoutParams(mParams)
        lastLeft = left
        lastRight = right
        lastTop = top
        lastBottom = bottom
    }

    fun hide() {
        if (left == 0 && top == 0 && right == 0 && bottom == 0) {
            mLeft = left
            mTop = top
            mRight = right
            mBottom = bottom
            return
        }

        moveLeft = left + moveMarginLR
        moveRight = right + moveMarginLR

        setRelativeViewLocation(moveLeft, top, moveRight, bottom)
        removeCallbacks(getMoneyMarginRunnable)
        postDelayed(getMoneyMarginRunnable, 300)
    }

    //延时任务
    private val getMoneyMarginRunnable: Runnable = object : Runnable {
        override fun run() {
            setRelativeViewLocation(left, top, right, bottom)
        }
    }
}
